<template>
	<div>
    <!-- 操作按钮 -->
    <div class="operatBtnWrapper">
        <button class="operatBtn marR5" @click.stop="add('form')" :disabled="!isFormOk" :class="{notAllowed:!isFormOk}">确认新建</button>
        <button class="operatBtn marR5" type="reset" @click.stop="resetForm('form')">清除内容</button>
        <!-- <button class="operatBtn" @click.stop="returnBack">返回上一页</button> -->
		</div>
    <!-- 表单 -->
		<el-form ref="form" :model="form" :rules="rules" class="paddLR20 marT10" :inline="true">
            <el-row>
                <el-col :span="6">
                <el-form-item label="职位名称" prop="posName">
                    <el-input v-model.trim="form.posName" class="inputTxtWrapper" maxlength="9" @input.stop.native="watchFrmChange"></el-input>
                </el-form-item>
                </el-col>
            </el-row>
		</el-form>
	</div>
</template>
<script>
  import http from '../../../../assets/js/http'
  // import fomrMixin from '../../../../assets/js/form_com'
  import _g from '@/assets/js/global'
 

  export default {
    data() {
      return {
        isFormOk:false,//表单默认没有通过校验

        isLoading: false,
        form: {
          posName: '',
        },
        rules: {
          posName: [
            { required: true, message: '请输入职位名称', trigger: 'blur' },
          ]
        }
      }
    },
    methods: {
    /* 侦听基本表单改变与校验是否通过 */ 
     watchFrmChange(frmName){
         this.isFormOk = true
     },

      /* 重置表单 */
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      /* 返回上一页  */
    //   returnBack(){
    //     this.$router.push('/admin/position/list')
    //   },
      /* 确认新建职位 */
      add(form) {
        const that = this
        this.$refs[form].validate((valid) => {                                              //  表单校验成功
          if (valid) {
              that.$confirm('是否确认新增职位?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning',
                  center: true
              }).then(() => {
                    _g.showLoading('提交中')
                    that.apiPost('admin/post/save', {...that.form}).then((res) => {
                        // console.log('res = ',res)
                         _g.showLoading().close()
                         if(typeof res === 'string'){
                            res = JSON.parse(res)
                          }
                        if (res.code != 200) {
                          that.handleError(res)
                        } else {
                          if(res.data.isSuccess === 0){             //  成功
                              _g.toastMsg("success", res.message)
                              that.$router.push('/admin/position/list')
                          }else{
                              _g.toastMsg("error", res.message)
                          }
                        }
                    })
              }).catch(() => {});
          }
        })
      },
    },
    mixins: [http]
  }
</script>